//
//  HMWelecomeController.swift
//  WymanWeibo
//
//  Created by wyman on 16/4/11.
//  Copyright © 2016年 wyman. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage
class HMWelecomeController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        prepareUI()
        
        setIcon()

        
        //发送网络请求
        HMUserAccountViewModel.shareInstance.loadUserInfo { (error) in
            if error != nil{
            HMPrint("获取用户信息出错\(error)")
            }
            
            //获取用户信息成功了
            self.setIcon()
        }
    }
//MARK: - 设置图片
    private func setIcon(){
    
        if let avatar_large = HMUserAccountViewModel.shareInstance.userAccount?.avatar_large{
        let url = NSURL(string: avatar_large)!
            self.iconView.sd_setImageWithURL(url, placeholderImage: UIImage.init(named: "avatar_default_big"))
            
        
        }
 
    
    }
    
    
    /**
     *  懒加载
     */
    private func prepareUI(){
    view.addSubview(bkgImageView)
        view.addSubview(iconView)
        view.addSubview(welLabel)
    
        //添加约束
        bkgImageView.translatesAutoresizingMaskIntoConstraints = false
        iconView.translatesAutoresizingMaskIntoConstraints = false
        welLabel.translatesAutoresizingMaskIntoConstraints = false
        
        bkgImageView.snp_makeConstraints { (make) in
            make.edges.equalTo(view)
        }
        
        iconView.snp_makeConstraints { (make) in
            make.centerX.equalTo(view)
            
            make.bottom.equalTo(view).offset(-160)
        }
        
        welLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(iconView)
            make.top.equalTo(iconView.snp_bottom).offset(16)
        }

    }
    
     /// 动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        iconViewAnimation()
        
    }
    
    //动画方法
    private func iconViewAnimation(){
    
    iconView.snp_updateConstraints { (make) in
        make.bottom.equalTo(view).offset(-(UIScreen.mainScreen().bounds.size.height - 160))
        }
        
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 5, options:  UIViewAnimationOptions(rawValue: 0), animations: { 
            self.view.layoutIfNeeded()
            }) { (_) in
                UIView.animateWithDuration(HMDefautlAnimDuration, animations: { () -> Void in
                    self.welLabel.alpha = 1
                    }, completion: { (_) -> Void in
//                        HMPrint("动画完成了,要切换界面")
                        let controller = HMMainViewController()
                        
                        //拿到代理对象
                        (UIApplication.sharedApplication().delegate as!AppDelegate).switchViewController(controller)
                        
                })
        }
    
    
    }
    
    private lazy var bkgImageView:UIImageView = UIImageView.init(image: UIImage.init(named: "ad_background") )
    
    private lazy var iconView:UIImageView = {
        let imageView =  UIImageView.init(image: UIImage.init(named: "avatar_default_big"))
        imageView.layer.cornerRadius = 42.5
        imageView.layer.masksToBounds = true
        return imageView
    }()
    private lazy var welLabel:UILabel = {
        var label = UILabel()
        label.text = "欢迎归来"
        label.textColor = UIColor.blackColor()
        label.font = UIFont.systemFontOfSize(14)
        label.sizeToFit()
        label.alpha = 0
    return label
    }()
    
}
